package com.suzhe.ymall.product.service.impl;

import com.suzhe.ymall.common.utils.RedisKeys;
import com.suzhe.ymall.common.utils.RedisTemplateUtils;
import com.suzhe.ymall.product.service.IProductEhcacheService;
import com.suzhe.ymall.product.service.IProductService;
import com.suzhe.ymall.product.service.ISpuService;
import com.suzhe.ymall.product.vo.SpuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ProductServiceImpl implements IProductService {
    @Autowired
    RedisTemplateUtils redisTemplateUtils;

    @Autowired
    IProductEhcacheService iProductEhcacheService;

    @Autowired
    ISpuService iSpuService;

    @Override
    public SpuVo getProductDetial(Integer id) {
        SpuVo eCacheSpuVo = iProductEhcacheService.get(id);
        if (eCacheSpuVo != null) {
            log.info("从ehcache中取到商品:{}", id);
            return eCacheSpuVo;
        }
        SpuVo redisSpuVo = (SpuVo) redisTemplateUtils.get(RedisKeys.getProductDetailKey(id));
        if (redisSpuVo != null) {
            log.info("从redis中取到商品:{}", id);
            return redisSpuVo;
        }
        log.info("从数据库中取商品信息:{}", id);
        return iSpuService.getSpuVo(id);
    }
}
